package hotel.callback.oversea.service.impl;

import hotel.callback.oversea.entity.meituan.MtHotelGoodsInfoNotifyLog;
import hotel.base.oversea.service.impl.BaseServiceImpl;
import hotel.base.oversea.utils.DateUtil;
import hotel.callback.oversea.mapper.MtHotelGoodsInfoNotifyLogMapper;
import hotel.callback.oversea.service.MtHotelOrderNotifyCallbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MtHotelOrderNotifyCallbackServiceImpl extends BaseServiceImpl implements MtHotelOrderNotifyCallbackService {

    @Autowired
    private MtHotelGoodsInfoNotifyLogMapper mtHotelGoodsInfoNotifyLogMapper;

    /**
     * 日活表前缀
     */
    public static final String USER_ACTIVE_TABLE_PREFIX = "mt_hotel_goods_info_notify_log_";
    public static final String USER_ACTIVE_TABLE_PREFIX_PRICE_INTERCEPT = "mt_hotel_goods_change_price_intercept_";

    /**
     * 批量新增
     * @param nowDate 例如:20201014
     * @param list
     * @return
     */
    @Override
    public int batchInsertList(String nowDate, List<MtHotelGoodsInfoNotifyLog> list){
        int retInt = 0;
        Date date = new Date();
        int hours = date.getHours();//获取当前小时 24小时制
        int min = date.getMinutes();
        if(hours == 23 && (min>40 && min <50)){//当天晚上11点40-11点50，判断是否创建隔天表
            createTableJobByDate(DateUtil.formatDate(DateUtil.getNextDays(1),"yyyyMMdd"));
        }

        retInt = mtHotelGoodsInfoNotifyLogMapper.batchInsertList(nowDate,list);

        return retInt;
    }


    /**
     * 定时器(每天11:30点执行一次，创建明天的表)
     */
    @Override
    public void createTableJobByDate(String date) {
        // 每次创建多张表（如果一次创建一张表，可能会有问题，比如由于未知原因，导致定时任务未执行，或者执行出错，此时表会创建失败）
        int num = 3;

        for(int i=1; i<=num; i++) {
//            String date = DateUtil.formatDate(DateUtil.getNextDays(1),"yyyyMMdd");
            this.createTable(date);
        }
    }

    /**
     * 获取表名
     * @param date
     * @return
     */
    public String getTableName(String date) {
        return USER_ACTIVE_TABLE_PREFIX + date;
    }
    public String getTableNamePriceIntercept(String date) {
        return USER_ACTIVE_TABLE_PREFIX_PRICE_INTERCEPT + date;
    }

    /**
     * 创建表
     * @param date
     */
    public void createTable(String date) {

        //美团异步推送价格表
        String sql2 = "create table if not exists " + getTableName(date) + "(\n" +
                " `id` varchar(40) NOT NULL,\n" +
                "  `mt_hotel_id` varchar(100) DEFAULT NULL,\n" +
                "  `mt_goods_id` varchar(100) DEFAULT NULL,\n" +
                "  `mt_goods_name` varchar(100) DEFAULT NULL,\n" +
                "  `mt_goods_status` tinyint(1) DEFAULT NULL,\n" +
                "  `mt_goods_type` tinyint(1) DEFAULT NULL,\n" +
                "  `mt_confirm_type` tinyint(1) DEFAULT NULL,\n" +
                "  `mt_average_price` varchar(100) DEFAULT NULL,\n" +
                "  `mt_original_price` varchar(100) DEFAULT NULL,\n" +
                "  `mt_third_party` tinyint(1) DEFAULT NULL,\n" +
                "  `mt_need_real_tel` tinyint(1) DEFAULT NULL,\n" +
                "  `goods_activity_map` text,\n" +
                "  `room_info_list` text,\n" +
                "  `break_fast` text,\n" +
                "  `book_rules` text,\n" +
                "  `cancel_rules` text,\n" +
                "  `price_models` text,\n" +
                "  `accesskey` varchar(40) DEFAULT NULL COMMENT '美团异步推送字段',\n" +
                "  `signature` varchar(40) DEFAULT NULL COMMENT '美团异步推送字段',\n" +
                "  `nonce` varchar(40) DEFAULT NULL COMMENT '美团异步推送字段',\n" +
                "  `timestamp` varchar(40) DEFAULT NULL COMMENT '美团异步推送字段',\n" +
                "  `state` tinyint(1) NOT NULL DEFAULT '0',\n" +
                "  `remark` longtext,\n" +
                "  `create_time` datetime DEFAULT NULL,\n" +
                "  `modify_time` datetime DEFAULT NULL,\n" +
                "  `price` int(11) DEFAULT NULL,\n" +
                "  `date` varchar(10) DEFAULT NULL,\n" +
                "  PRIMARY KEY (`id`),\n" +
                "  KEY `index_mt_goods_id` (`mt_goods_id`)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;";
        mtHotelGoodsInfoNotifyLogMapper.createTable(sql2);

        //美团异步1小时内频繁变动日志表
        String sql = "create table if not exists " + getTableNamePriceIntercept(date) + "(\n" +
                "`id` varchar(50) NOT NULL COMMENT '主键',"+
                "`mt_hotel_id` varchar(40) NOT NULL COMMENT '美团酒店id',"+
                "`mt_goods_id` varchar(40) NOT NULL COMMENT '美团产品id',"+
                "`mt_date` varchar(12) DEFAULT NULL COMMENT '日期',"+
                "`mt_price` int(11) DEFAULT NULL COMMENT '价格',"+
                "`mt_accesskey` varchar(40) DEFAULT NULL COMMENT '美团异步推送的key',"+
                "`mt_signature` varchar(40) DEFAULT NULL COMMENT '美团异步推送的key',"+
                "`mt_nonce` varchar(40) DEFAULT NULL COMMENT '美团异步推送的key',"+
                "`mt_timestamp` varchar(40) DEFAULT NULL COMMENT '美团异步推送的timestamp',"+
                "`remark` varchar(100) DEFAULT NULL,"+
                "`create_time` datetime DEFAULT NULL COMMENT '创建时间',"+
                "KEY `index_mt_hotel_id` (`mt_hotel_id`) ,"+
                "KEY `index_mt_goods_id` (`mt_goods_id`) ,"+
                "KEY `index_mt_date` (`mt_date`) USING BTREE "+
        ") ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='美团异步一小时内频繁变动的价格的酒店记录';";



        mtHotelGoodsInfoNotifyLogMapper.createTable(sql);
    }

}
